package com.qf.web.sys_login.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.config.jwt.JwtUtil;
import com.qf.utils.ResultUtil;
import com.qf.utils.ResultVo;
import com.qf.web.sys_login.entity.LoginParams;
import com.qf.web.sys_login.entity.LoginResult;
import com.qf.web.sys_role.entity.Role;
import com.qf.web.sys_user.entity.User;
import com.qf.web.sys_user.service.UserService;
import com.qf.web.sys_user_role.entity.UserRole;
import com.qf.web.sys_user_role.service.UserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author xufuhan
 * @Date 2024/9/11 18:11
 */

@RestController
@RequestMapping("/api/login")
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public ResultVo login(@RequestBody LoginParams params) {
        if (StringUtils.isEmpty(params.getUsername()) || StringUtils.isEmpty(params.getPassword()) || StringUtils.isEmpty(String.valueOf(params.getUserType()))) {
            return ResultUtil.error("用户名或密码不能为空");
        }

        //获取加密后的密码
        String pass = DigestUtils.md5DigestAsHex(params.getPassword().getBytes());
        //组装条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUsername, params.getUsername())
                .eq(User::getPassword, pass);
        User user = userService.getOne(wrapper);
        if (user == null) {
            return ResultUtil.error("用户名或密码错误或者不存在改类型的用户");
        }
        UserRole userRole = new UserRole();
        userRole.setRoleId(params.getUserType());
        QueryWrapper<UserRole> wrapper2 = new QueryWrapper<>();
        wrapper2.lambda().eq(UserRole::getUserId, user.getUserId());
        UserRole dbuserRole = userRoleService.getOne(wrapper2);
        if (!dbuserRole.getRoleId().equals(userRole.getRoleId())){
            return ResultUtil.error("没有该类型的用户");
        }
        //生成token
        Map<String, String> map = new HashMap<>();
        map.put("userId", user.getUserId().toString());
        map.put("username", user.getUsername());

        String token = jwtUtil.generateToken(map);
        //设置返回值
        LoginResult result = new LoginResult();
        result.setToken(token);
        result.setUserId(user.getUserId());
        result.setUsername(user.getUsername());
        return ResultUtil.success("登陆成功", result);
    }
}
